<%@ page contentType="text/html; charset=utf-8" language="java"  %>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%>
<%@ taglib prefix="shiro" uri="http://shiro.apache.org/tags" %>
<!DOCTYPE html>
<html>
	<head>
		<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
		<%@include file="/static/common/header.jsp"%>
		<%@include file="/static/common/easyui.jsp"%>
		<script src="${ctx}/static/jquery/jquery.form.js" type="text/javascript"></script>
		<link href="${ctx}/static/css/main.css" rel="stylesheet">
		<script>
		
		var options = { 
			 	beforeSubmit: progressing,
		        contentType: "application/x-www-form-urlencoded;charset=utf-8", 
				dataType:"json",  
		        success: function(jsonStr){	
					 progressed();			 
					if(jsonStr.STATE=='ERROR'){
							showInfo("操作失败:"+jsonStr.INFO);
					}else{
						$('#dicEditWin').window('close');	
						reloadTreeGridData();
						reloadUpDicIdCombobox();
						showInfo();
					}
				},
				resetForm:true
		    };
		
		//重新加载数据
		function reloadTreeGridData(){
			$('#dicTreeGrid').treegrid('reload');
			reloadUpDicIdCombobox();
		}
		
		
		
		//删除
		function delDicBut(dicId) {
			$.messager.confirm('确认', '确认删除所选?', function(r){ if (r){
				$.ajax({
					type:"post", 
					cache: false, 
					dataType:"json", 
					async:false,
					url: "${ctx}/common/dic/del",
					data: "id="+dicId,
					beforeSend:function(){ progressing()}, 
					complete:function(){progressed()},
					success:function(jsonStr){
						if(jsonStr.STATE=='ERROR'){
							showInfo("操作失败:"+jsonStr.INFO);
						}else{
							reloadTreeGridData();
							showInfo();
						}
					}
				});
			}});
		}
		
		//重新加载一级字典
		function reloadUpDicIdCombobox(){
			$('#upDicIdCombobox').combobox('setValue','');
			$('#upDicIdCombobox').combobox('reload', '${ctx}/common/dic/topDicData')
		}
		
		//获取字典ID
		function getSelectedId(){
			var row = $('#dicTreeGrid').datagrid('getSelected');
			if (row){
				return row.id;
			}else{
				return "";
			}
		}
		
		//字典级别修改
		function levelSet(){
		 	if($('#upDicIdCombobox').combobox('getValue')=="1")
				$('#upDicIdCombobox').combobox('setValue','');
		 	if($("#dicLevel").val()=="1"){
				$("#mcbox").css("display","none");
			}else{
				$("#mcbox").css("display","");
			}
		 }
		
		//保存字典信息
		function toUpdateDic(){
			var isValid = $("#dicEditForm").form('validate');
			
			if($("#dicLevel").val()!="1" && $('#upDicIdCombobox').combobox('getValue')==""){
				isValid=false;
				showInfoCenter("请选择上级字典");
			}
			if($("#id").val()!=""&&($("#id").val() == $('#upDicIdCombobox').combobox('getValue'))){
				showInfoCenter("请重新选择上级字典！");
				isValid=false;
			}
			if (!isValid){
				$.messager.progress('close');
			}
			if(isValid){
				$('#dicEditForm').ajaxSubmit(options);
			}else{
				$.messager.progress('close');
			}
			
		}
		
		//跳转新增或修改页面
		function modify(OPT){
		 	$('#dicEditForm').resetForm();	
			$("#id").val("");
			 var mId=getSelectedId();
			 if(OPT=='N'){
			 	mId="";
			 	$("#mcbox").css("display","none");
			 }else if(mId==""){
			 	showInfoCenter("请选择要修改的数据！");
				return;
			 }
			 $('#dicEditWin').window('open');
			 
			 if(mId!=''){
				 $.ajax({
					type:"post",
					cache: false,
					dataType:"json",
					async:false,
					url: "${ctx}/common/dic/detail",
					data: "id="+mId,
					beforeSend:function(){ progressing()}, 
					complete:function(){progressed()},
					success:function(jsonStr){
						reloadUpDicIdCombobox();
						if(jsonStr.STATE=='ERROR'){
							showInfo("操作失败:"+jsonStr.INFO);
						}else{
							$("#dicEditForm").form("load",jsonStr);
							levelSet();
						}
					}
				});
			}
		 }
		</script>
	</head>
	
	<body id="orgLayOut" class="easyui-layout">
		<div data-options="region:'center',collapsed:false,border:false" class="bmargin">
				<table id="dicTreeGrid" class="easyui-treegrid" title="系统字典"  
					data-options="
						url: '${ctx}/common/dic/treeGridData',
						rownumbers: true,
						idField: 'id',
						treeField: 'dicName',
						fit: true,
						toolbar:'#tb'
					">
					<thead>
						<th data-options="field:'dicName'" width="300">字典名称</th>
						<th data-options="field:'dicValue'" width="300">字典编码</th>
						<th data-options="field:'dicValue2'" width="200">字典编码2</th>
						<th data-options="field:'dicValue3'" width="200">字典编码3</th>
						<th data-options="field:'optBut'" width="200">操作</th>
					</thead>
				</table>
		</div>
		
		<div id="tb" class="datagrid-toolbar" style="padding:5px;height:auto">
			<div>
				<shiro:hasPermission name="____AgentAdmin">
            		<a href="javascript:void(0)" onClick="modify('N')" id="btnadd" class="easyui-linkbutton" iconCls="icon-add" plain="true">添加</a>
            		<a href="javascript:void(0)" onClick="modify('E')" id="btnedit" class="easyui-linkbutton" iconCls="icon-edit" plain="true">更新</a>
            		<a href="javascript:void(0)" onClick="reloadTreeGridData();" id="btnedit" class="easyui-linkbutton" iconCls="icon-reload" plain="true">刷新</a>
        		</shiro:hasPermission>
        	</div>
		</div>
		
		<div id="dicEditWin" class="easyui-window" title="Modal Window" data-options="modal:true,closed:true,iconCls:'icon-edit',title:'字典维护'" style="width:580px;height:320px;padding:10px;">
			<form method="post" id="dicEditForm" action="${ctx}/common/dic/updateDic">
				<input type="hidden" name="id" id="id" value=""/>
				<table  class="listTable" align="center" width="95%">
      				<tr height="30">
      					<td align="left">字典级别：</td>
      					<td align="left">
					  		<select id="dicLevel" name="dicLevel" onChange="levelSet()" style="width: 150px;">
								<option value="1" >一级</option>
								<option value="2" >二级</option>
							</select>
      					</td>
      				</tr>
      				<tr height="30" id="mcbox" style="display:none;">
      					<td align="left"> 上级字典： </td>
      					<td align="left">
					  		<select class="easyui-combobox" data-options="valueField:'id',textField:'dicName',editable:false,url:'${ctx}/common/dic/topDicData',editable:false" style="width: 150px;" name="upDicId" id="upDicIdCombobox"></select>
      					</td>
      				</tr>
      				<tr height="30">
      					<td align="left"> 字典名称： </td>
      					<td align="left">
      				  		<input type="text" id="dicName" name="dicName" value="" class="easyui-validatebox" data-options="required:true"/>
      					</td>
      				</tr>
      				<tr height="30">
      					<td align="left"> 字典编码： </td>
      					<td align="left">
      				  		<input type="text" id="dicValue" name="dicValue" value="" class="easyui-validatebox" data-options="required:true"/>
      					</td>
      				</tr>
      				<tr height="30">
      					<td align="left"> 字典编码2： </td>
      					<td align="left">
      				  		<input type="text" id="dicValue2" name="dicValue2" value="" class="easyui-validatebox" />
      					</td>
      				</tr>
      				<tr height="30">
      					<td align="left"> 字典编码3： </td>
      					<td align="left">
      				  		<input type="text" id="dicValue3" name="dicValue3" value="" class="easyui-validatebox" />
      					</td>
      				</tr>
      				<tr height="30">
      					<td align="left"> 状态： </td>
      					<td align="left">
      				  		<select id="used" name="used">
								<option value="1" >启用</option>
								<option value="0" >禁用</option>
							</select>
      					</td>
      				</tr>
      				<tr height="30">
      					<td align="left"> 排序： </td>
      					<td align="left">
      				  		<input class="easyui-validatebox" type="text" name="sort" id="sort" data-options="required:true"/>
      					</td>
      				</tr>
      				<tr height="50">
      					<td colspan="4" align="center">
      				  		<a href="javascript:void(0);" id="btn-back"
								onclick="$('#dicEditWin').window('close');" class="easyui-linkbutton" iconCls="icon-back">返回</a> 
							<a href="javascript:void(0);" id="btn-add"
								onclick="toUpdateDic()" class="easyui-linkbutton" iconCls="icon-save">保存</a> 
						</td>
      				</tr>
				</table>
			</form>
		</div>
		
	</body>
</html>